package com.lyh.hkcloud.service;

import android.app.Activity;
import android.util.Log;
import com.lyh.hkcloud.constants.UrlConstants;
import com.lyh.hkcloud.module.common.ModuleService;
import com.lyh.hkcloud.util.base.JsonUtil;
import com.lyh.hkcloud.util.base.ToastUtils;
import com.lyh.hkcloud.util.http.Request;
import com.lyh.hkcloud.util.http.listener.json.JsonFailuredListener;
import com.lyh.hkcloud.util.http.listener.json.JsonSuccessListener;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class LogisticsBillService extends ModuleService {


    public LogisticsBillService(Activity activity) {
        super(activity);
    }

    public void findListZ63(JSONObject jsonObject,  final JsonSuccessListener successListener, final JsonFailuredListener failuredListener) {
        Request.post(activity, UrlConstants.BUS_ROOT + "/exportGoodsBusiness/findListZ63", jsonObject, new JsonSuccessListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                handlerRequestData(msg, response, successListener, failuredListener);
            }
        }, new JsonFailuredListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                Log.e("error",response.optString("message"));
                handlerFailured(msg, response, failuredListener);
            }
        });
    }

    public void doPostZ63(final JSONObject jsonObject, final JsonSuccessListener successListener, final JsonFailuredListener failuredListener) {
        Request.post(activity, UrlConstants.BUS_ROOT + "/exportGoodsBusiness/doPostZ63", jsonObject, new JsonSuccessListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                handlerRequestData(msg, response, successListener, failuredListener);
            }
        }, new JsonFailuredListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                Log.e("error",response.optString("message"));
                handlerFailured(msg, response, failuredListener);
            }
        });
    }

    public void findListZ64(JSONObject jsonObject,  final JsonSuccessListener successListener, final JsonFailuredListener failuredListener) {
        Request.post(activity, UrlConstants.BUS_ROOT + "/exportGoodsBusiness/findListZ64", jsonObject, new JsonSuccessListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                handlerRequestData(msg, response, successListener, failuredListener);
            }
        }, new JsonFailuredListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                Log.e("error",response.optString("message"));
                handlerFailured(msg, response, failuredListener);
            }
        });
    }

    public void doPostZ64(final JSONObject jsonObject, final JsonSuccessListener successListener, final JsonFailuredListener failuredListener) {
        Request.post(activity, UrlConstants.BUS_ROOT + "/exportGoodsBusiness/doPostZ64", jsonObject, new JsonSuccessListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                handlerRequestData(msg, response, successListener, failuredListener);
            }
        }, new JsonFailuredListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                Log.e("error",response.optString("message"));
                handlerFailured(msg, response, failuredListener);
            }
        });
    }

    public void findListZ65(JSONObject jsonObject,  final JsonSuccessListener successListener, final JsonFailuredListener failuredListener) {
        Request.post(activity, UrlConstants.BUS_ROOT + "/logisticsBill/findListZ65", jsonObject, new JsonSuccessListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                handlerRequestData(msg, response, successListener, failuredListener);
            }
        }, new JsonFailuredListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                Log.e("error",response.optString("message"));
                handlerFailured(msg, response, failuredListener);
            }
        });
    }

    public void doPostZ65(final JSONObject jsonObject, final JsonSuccessListener successListener, final JsonFailuredListener failuredListener) {
        Request.post(activity, UrlConstants.BUS_ROOT + "/logisticsBill/doPostZ65", jsonObject, new JsonSuccessListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                handlerRequestData(msg, response, successListener, failuredListener);
            }
        }, new JsonFailuredListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                Log.e("error",response.optString("message"));
                handlerFailured(msg, response, failuredListener);
            }
        });
    }

    public void findListZ66(JSONObject jsonObject,  final JsonSuccessListener successListener, final JsonFailuredListener failuredListener) {
        Request.post(activity, UrlConstants.BUS_ROOT + "/logisticsBill/findListZ66", jsonObject, new JsonSuccessListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                handlerRequestData(msg, response, successListener, failuredListener);
            }
        }, new JsonFailuredListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                Log.e("error",response.optString("message"));
                handlerFailured(msg, response, failuredListener);
            }
        });
    }

    public void doPostZ66(final JSONObject jsonObject, final JsonSuccessListener successListener, final JsonFailuredListener failuredListener) {
        Request.post(activity, UrlConstants.BUS_ROOT + "/logisticsBill/doPostZ66", jsonObject, new JsonSuccessListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                handlerRequestData(msg, response, successListener, failuredListener);
            }
        }, new JsonFailuredListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                Log.e("error",response.optString("message"));
                handlerFailured(msg, response, failuredListener);
            }
        });
    }

    public void findListZ67(JSONObject jsonObject,  final JsonSuccessListener successListener, final JsonFailuredListener failuredListener) {
        Request.post(activity, UrlConstants.BUS_ROOT + "/exportGoodsBusiness/findListZ67", jsonObject, new JsonSuccessListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                handlerRequestData(msg, response, successListener, failuredListener);
            }
        }, new JsonFailuredListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                Log.e("error",response.optString("message"));
                handlerFailured(msg, response, failuredListener);
            }
        });
    }

    public void doPostZ67(final JSONObject jsonObject, final JsonSuccessListener successListener, final JsonFailuredListener failuredListener) {
        Request.post(activity, UrlConstants.BUS_ROOT + "/exportGoodsBusiness/doPostZ67", jsonObject, new JsonSuccessListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                handlerRequestData(msg, response, successListener, failuredListener);
            }
        }, new JsonFailuredListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                Log.e("error",response.optString("message"));
                handlerFailured(msg, response, failuredListener);
            }
        });
    }

    public void findListZ68(JSONObject jsonObject,  final JsonSuccessListener successListener, final JsonFailuredListener failuredListener) {
        Request.post(activity, UrlConstants.BUS_ROOT + "/logisticsBill/findListZ68", jsonObject, new JsonSuccessListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                handlerRequestData(msg, response, successListener, failuredListener);
            }
        }, new JsonFailuredListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                Log.e("error",response.optString("message"));
                handlerFailured(msg, response, failuredListener);
            }
        });
    }

    public void doPostZ68(final JSONObject jsonObject, final JsonSuccessListener successListener, final JsonFailuredListener failuredListener) {
        Request.post(activity, UrlConstants.BUS_ROOT + "/logisticsBill/doPostZ68", jsonObject, new JsonSuccessListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                handlerRequestData(msg, response, successListener, failuredListener);
            }
        }, new JsonFailuredListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                Log.e("error",response.optString("message"));
                handlerFailured(msg, response, failuredListener);
            }
        });
    }

    public void getValidBarcode(String mjahr, String imOclas, String psnCode, String psnName, final JsonSuccessListener successListener, final JsonFailuredListener failuredListener) {
        JSONObject jsonObject = new JSONObject();

        try {
            jsonObject.put("imBarcode", mjahr);
            jsonObject.put("imOclas", imOclas);
            jsonObject.put("cardNo", psnCode);
            jsonObject.put("psnName", psnName);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        Request.post(activity, UrlConstants.BUS_ROOT + "/divertBusiness/getValidBarcode", jsonObject, new JsonSuccessListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                if (!"0000".equals(response.optString("code"))) {
                    ToastUtils.makeText(activity, response.optString("msg"), ToastUtils.LENGTH_SHORT).show();
                    failuredListener.onRespone(response.optString("msg"), null);
                } else {
                    successListener.onRespone(response.optString("msg"), JsonUtil.getJSONObject(response, "data", JsonUtil.getBlankJSONObject()));
                }
            }
        }, new JsonFailuredListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                Log.e("error",response.optString("message"));
                ToastUtils.makeText(activity, msg, ToastUtils.LENGTH_SHORT).show();
                failuredListener.onRespone(response.optString("msg"), response);
            }
        });
    }

    private void handlerRequestData(String msg, JSONObject response,final JsonSuccessListener successListener,final JsonFailuredListener failuredListener ){
        final JSONObject testData = new JSONObject();
        try {
            JSONArray jsonArray = new JSONArray();
            JSONObject item = new JSONObject();
            item.put("delivnum", "3002102300");
            item.put("mblnr", "3002102300");  //物料凭证编号
            item.put("zeile", "21");  //物料凭证中的项目
            item.put("ebeln", "p33213");  //采购订单编号
            item.put("ebelp", "c2312010");  //采购凭证的项目编号
            item.put("lifnr", "广东利元亨");  //供应商账户
            item.put("bukrs", "2299");  //公司代码
            item.put("werks", "2288");
            item.put("lgort", "21");
            item.put("werkt", "1288");
            item.put("lgott", "29");
            item.put("matnr", "400102031023");
            item.put("maktx", "woe物料描述。。发");
            item.put("aufnr", "900020012023");
            item.put("erfmg", 100);
            item.put("mincount", "10");
            item.put("menge", "10");
            item.put("erfme", "PCS");
            item.put("prodbatch", "955-021");
            item.put("workp", "00-02");
            item.put("prodbatct", "911-020");
            item.put("workt", "01-01");
            item.put("ngeln", "ecn");
            item.put("ngelnt", "disecn");
            item.put("retext", "remark");
            jsonArray.put(item);
            testData.put("title", JsonUtil.getBlankJSONObject());
            testData.put("data", jsonArray);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        if (!"0000".equals(response.optString("code"))) {
            ToastUtils.makeText(activity, response.optString("msg"), ToastUtils.LENGTH_SHORT).show();
            failuredListener.onRespone(response.optString("msg"), null);
//            successListener.onRespone("success", testData);
        } else {
            successListener.onRespone(response.optString("msg"), JsonUtil.getJSONObject(response, "data", JsonUtil.getBlankJSONObject()));
        }
    }

    private void handlerFailured(String msg, JSONObject response,final JsonFailuredListener failuredListener ){
        ToastUtils.makeText(activity, msg, ToastUtils.LENGTH_SHORT).show();
        failuredListener.onRespone(response.optString("msg"), null);
    }
}
